package com.uaroadshow.finale.web.controller.verification;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.utils.CollectionUtils;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.core.wechat.entity.cfg.JsConfig;
import com.uaroadshow.entity.trade.Product;
import com.uaroadshow.entity.user.Staff;
import com.uaroadshow.entity.user.User;
import com.uaroadshow.entity.user.UserCourse;
import com.uaroadshow.finale.processor.WechatProcessor;
import com.uaroadshow.finale.service.DataService;
import com.uaroadshow.finale.service.ProductService;
import com.uaroadshow.finale.service.UserCourseService;
import com.uaroadshow.finale.web.interceptor.UserContext;
import com.uaroadshow.finale.web.interceptor.UserContextType;
import com.uaroadshow.finale.web.resolver.Secure;

/**
 *
 * @author Ark
 *
 */
@Controller
@RequestMapping("/staffs")
public class StaffController {
    
    @Autowired
    private WechatProcessor wechatProcessor;

    @Autowired
    @Qualifier("dataService")
    private DataService<Staff> staffService;
    
    @Autowired
    private UserCourseService userCourseService;
    
    @Autowired
    private ProductService productService;
    
    /**
     * staff login
     * 
     * @param user
     * @param courseId
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    @UserContext(type = UserContextType.WECHAT_AUTHORITY)
    public String login(@Secure User user, @RequestParam("courseId") Long courseId) {
        
        String mobile = user.getMobile();
        if (StringUtils.isNotBlank(mobile)) {
            
            // 校验白名单
            Staff staff = staffService.load(Staff.class, "mobile", mobile);
            if (staff == null) {
                return "staff/error-page";
            }
            
            // 进入核销页
            return "redirect:/staffs/verificate?courseId=" + courseId;
            
        } else {
            // 绑定手机，非核销人员无法绑定成功
            return "redirect:/staffs/bind-mobile?courseId=" + courseId;
        }
        
    }
    
    /**
     * @param user
     * @param request
     * @return
     */
    @RequestMapping(value = "/bind-mobile", method = RequestMethod.GET)
    @UserContext(type = UserContextType.WECHAT_AUTHORITY)
    public ModelAndView bindMobile(@Secure User user,
                                   @RequestParam("courseId") Long courseId,
                                   HttpServletRequest request) {
        
        ModelAndView modelAndView = new ModelAndView("staff/bind-mobile");
        
        String curl = WebUtils.getRealRequestURL(request);
        JsConfig jsConfig = wechatProcessor.generateJsConfig(curl);
        modelAndView.addObject("jsConfig", JsonUtils.toJson(jsConfig));

        @SuppressWarnings("unchecked")
        List<String> mobiles = CollectionUtils.extractPropertyList(staffService.listAll(Staff.class), "mobile");
        modelAndView.addObject("mobiles", mobiles);
        
        modelAndView.addObject("user", user);
        modelAndView.addObject("courseId", courseId);
        
        return modelAndView;
        
    }
    
    /**
     * @param user
     * @param courseId
     * @param request
     * @return
     */
    @RequestMapping(value = "/verificate", method = RequestMethod.GET)
    @UserContext(type = UserContextType.WECHAT_AUTHORITY)
    public ModelAndView verificate(@RequestParam("courseId") Long courseId,
                                   HttpServletRequest request) {
        
        ModelAndView modelAndView = new ModelAndView("staff/staff-verificate");
        
        String curl = WebUtils.getRealRequestURL(request);
        JsConfig jsConfig = wechatProcessor.generateJsConfig(curl);
        modelAndView.addObject("jsConfig", JsonUtils.toJson(jsConfig));
        
        UserCourse userCourse = userCourseService.load(UserCourse.class, courseId);
        modelAndView.addObject("userCourse", userCourse);
        
        Product product = userCourse.getProduct();
        if (product.isParent()) {
            List<Product> products = productService.list(product);
            modelAndView.addObject("products", products);
        }
        
        return modelAndView;
        
    }
    
}
